package com.xunyun.kotlinwc.network.interceptor

import android.util.Log
import com.xunyun.kotlinwc.BuildConfig
import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.ResponseBody.Companion.toResponseBody

/**
 * @author gl
 * @date 2021/9/15
 * @desc
 */
class SignInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val originRequest = chain.request()
        val requestBuilder = originRequest.newBuilder()
        // 可以添加公共请求参数
        val request = requestBuilder.build()
        val response = chain.proceed(request)
        val body = response.body;
        if (null != response) {
            val mediaType = body?.contentType()
            val content = body?.string()
            if ("debug" == BuildConfig.BUILD_TYPE) {
                mediaType?.let {
                    Log.e("mediaType==", it.type)
                }
                content?.let {
                    Log.e("response==", it)
                }
            }

            return response.newBuilder().body(content?.let { it.toResponseBody(mediaType) })
                .build()

        }

        return response;

    }
}